package com.sujiewon.salesadmin.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * SHA1 Encrypt Algorithm
 * 
 * @author Park Sang Jun
 *
 */
public class EncryptUtil {

	/**
	 * SHA1 Encrypt Algorithm
	 * 
	 * @param text
	 * @return encryptedText
	 * @throws Exception 
	 */
	public static String encryptToSha1(String text) throws Exception{
		byte[] bytes = text.getBytes();
		MessageDigest messageDigest = null;
		
		try{
			messageDigest = MessageDigest.getInstance("SHA1");
		} catch(NoSuchAlgorithmException e){
			e.printStackTrace();
		}
		if(messageDigest == null){
			throw new Exception("ERROR FOR ENCRYPT PROCESS :: MessageDigest is null.");
		}
		messageDigest.reset();
		messageDigest.update(bytes);
		
		byte[] digest = messageDigest.digest();
		StringBuffer encryptedText = new StringBuffer();
		for(int i=0; i<digest.length; i++){
			encryptedText.append(Integer.toHexString(0xFF & digest[i]));
		}
		
		return encryptedText.toString();
	}
}
